// pages/my/my.js
import { login, sign } from '../../utils/http'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        user: null,
        token: null,
        uid: null,
        sho: true,
        icon: [
            {
                ico: 'pending-payment',
                active: 'a',
                title: '待支付'
            },
            {
                ico: 'cash-on-deliver',
                active: 'b',
                title: '待发货'
            },
            {
                ico: 'logistics',
                active: 'c',
                title: '待收货'
            },
            {
                ico: 'records',
                active: 'd',
                title: '待评价'
            },
            {
                ico: 'contact',
                active: 'e',
                title: '售后'
            },
        ]
    },
    // 登录
    getUserInfo() {
        wx.login({
            success: (res) => {
                var obj = {}
                obj.code = res.code
                login(obj).then(res => {
                    if (res.code == 10000) {
                        wx.login({
                            success: (res) => {
                                sign({
                                    code: res.code,
                                    encryptedData: this.data.user.encryptedData,
                                    iv: this.data.user.iv
                                })
                                this.getUserInfo()
                            },
                        });
                    } else {
                        wx.setStorageSync('token', res.data.token);
                        wx.setStorageSync('uid', res.data.uid);
                        this.setData({
                            token: res.data.token,
                            uid: res.data.uid
                        })
                        this.store()
                    }
                })
            },
        });
    },
    // 退出登录
    retreat() {
        wx.removeStorageSync('token');
        wx.removeStorageSync('uid');
        this.setData({
            user: null,
            token: null,
            sho: true
        })
    },
    // 暂时不登陆
    cancel() {
        this.setData({
            sho: false
        })
    },
    // 允许
    allow() {
        this.getUserInfo()
        this.store()
        this.setData({
            sho: false
        })
    },
    // 检测本地数据
    store() {
        let user = wx.getStorageSync("user");
        if (user) {
            this.setData({
                user: JSON.parse(wx.getStorageSync("user"))
            })
        } else {
            wx.getUserInfo({
                success: (res) => {
                    this.setData({
                        user: res
                    })
                    wx.setStorageSync("user", JSON.stringify(res));
                },
            });
        }
    },
    affair(e) {
        this.setData({
            token: e.detail.token,
            uid: e.detail.uid,
            user: e.detail.user
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.store()
        let token = wx.getStorageSync("token");
        let uid = wx.getStorageSync("uid");
        if (token) {
            this.setData({
                sho: false,
                token,
                uid
            })
        } else {
            this.setData({
                sho: true
            })
        }

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.store()
        let token = wx.getStorageSync("token");
        let uid = wx.getStorageSync("uid");
        if (token) {
            this.setData({
                sho: false,
                token,
                uid
            })
        } else {
            this.setData({
                sho: true
            })
        }

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        this.retreat()
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})